﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CMS.Utils
{
    /// <summary>
    /// 加密解密工具
    /// </summary>
    public class EncryptUtil
    {
        /// <summary>
        /// 得到一个SHA256加密的字符串
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string SHA256(string Text)
        {
            var message = Encoding.ASCII.GetBytes(Text);
            var hashString = new SHA256Managed();
            var hashValue = hashString.ComputeHash(message);
            return hashValue.Aggregate("", (current, x) => current + $"{x:x2}");
        }

        /// <summary>
        /// 得到一个MD5加密的字符串
        /// </summary>
        /// <param name="Text">原始字符串</param>
        /// <returns>加密后字符串</returns>
        public static string MD5(string Text)
        {
            return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(Text))).Replace("-", "");
        }

        /// <summary>
        /// 固定可逆加密盐值
        /// </summary>
        private const string SaltValue = "XWECHATSALTVALUE";

        /// <summary> 
        /// 可逆加密 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(string Text)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(SaltValue).Substring(2, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(SaltValue).Substring(2, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        /// <summary> 
        /// 可逆解密
        /// </summary> 
        /// <param name="Text"></param> 
        /// <returns></returns> 
        public static string Decrypt(string Text)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(SaltValue).Substring(2, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(SaltValue).Substring(2, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
    }
}


